package com.tza.web.t_net.client.sceens;

/*
 * SmartGWT (GWT for SmartClient) Copyright 2008 and beyond, Isomorphic
 * Software, Inc. SmartGWT is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. SmartGWT is also available
 * under typical commercial license terms - see http://smartclient.com/license
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tza.web.t_net.client.ActionContext;
import com.tza.web.t_net.client.screenservices.DataProviderService;
import com.tza.web.t_net.client.screenservices.DataProviderServiceAsync;
import com.tza.web.t_net.shared.entity.Data;
import com.tza.web.t_net.shared.entity.DataMap;

public class SimpleTreeGrid extends Tree
{
   private final DataProviderServiceAsync dataProviderService = GWT.create(DataProviderService.class);
   ActionContext                          actionContext;
   private String                         gridServiceBeanId;
   private static String                  childrenProperty;
   private static String                  nameProperty;
   private Tree                           dataTree;
   private TreeItem                       treeRoot;

   public SimpleTreeGrid(DataSource simpleTreeGridDS)
   {

   }

   public SimpleTreeGrid(ActionContext mainActionContext)
   {
      actionContext = mainActionContext;

   }

   public void LoadData(String childrenNodeProperty, String nodeNameProperty, String beanId)
   {

      childrenProperty = childrenNodeProperty;
      nameProperty = nodeNameProperty;
      dataTree = new Tree();

      // dataTree.setShowRoot(true);

      final ListGridRecord record = new ListGridRecord();
      DataMap criteria = null;
      dataProviderService.getTreeGridData(beanId, criteria, new AsyncCallback<DataMap>()
      {

         public void onFailure(Throwable caught)
         {
            Window.alert("Failure On RPC Call LoadData");
         }

         public void onSuccess(DataMap result)
         {

            treeRoot = new DataTreeNode(result);
            dataTree.addItem(treeRoot);

         }

      });

   }

   public static class DataTreeNode extends TreeItem
   {

      public DataTreeNode(DataMap node)
      {
         System.out.println(node.get(nameProperty) + " 0");
         ArrayList<DataTreeNode> childs = new ArrayList<DataTreeNode>();
         if (node != null)
         {

            System.out.println(node.get(nameProperty) + " ");
            for (Data data : node.getDataList())
            {
               if (!(data.getValue() instanceof DataMap))
               {
                  if (nameProperty.equals(data.getKey())) System.out.println(node.get(nameProperty) + " " + data.getKey() + " " + data.getValue());
                  addItem((String) data.getValue());
               }
            }
            childs = new ArrayList<DataTreeNode>();
            for (Data data : node.getDataList())
            {
               if (data.getValue() != null && data.getValue() instanceof DataMap)
               {

                  childs.add(new DataTreeNode((DataMap) data.getValue()));
                  addItem(new DataTreeNode((DataMap) data.getValue()));

               }

            }
            if (childs != null && childs.size() > 0)
            {
               // addItem(childrenProperty, childs.toArray());
               System.out.println(node.get(nameProperty) + " " + childs.size() + " " + childrenProperty);
            }

         }

      }

   }

}
